package com.libl.controller;

import com.libl.service.StatService;
import com.libl.util.ResponseUtil;
import com.libl.vo.StatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/stat")
public class DtsCountController {

    @Autowired
    StatService statService;
    //用户统计
    @GetMapping("/user")
    public Object user(){
        StatVo statVo = new StatVo();
        String[] columns = {"day","users"};
        statVo.setColumns(columns);
        String[] statVoColumns = statVo.getColumns();
        //查询所有天数
        List<String> days = statService.findUserDay();
        //遍历days
        for (String day : days) {
            //通过day查询每天的用户增长数
            Integer users = statService.findUsersByDay(day);

            Map<String, Object> rows = new HashMap<>();
            rows.put(statVoColumns[0],day);
            rows.put(statVoColumns[1],users);

            statVo.add(rows);
        }

        HashMap<String, Object> data = new HashMap<>();
        data.put("columns",statVo.getColumns());
        data.put("rows", statVo.getRows());

        return ResponseUtil.ok(data);
    }

    //订单统计
    @GetMapping("/order")
    public Object order(){
        StatVo statVo = new StatVo();
        String[] columns = {"day","orders","customers","amount","pcr"};
        statVo.setColumns(columns);
        String[] statVoColumns = statVo.getColumns();
        //查询所有天数
        List<String> days = statService.findOrderDay();
        //遍历days
        for (String day : days) {
            //通过day查询每天的订单量
            List<Integer> order = statService.findOrdersByDay(day);
            //通过day查询每天的下单用户数
            List<Integer> customer = statService.findCustomersByDay(day);
            //通过day查询每天的订单总额
            List<Double> amount = statService.findAmountByDay(day);
            //通过day查询每天的客单价
            List<Double> pcr = statService.findPcrByDay(day);

            Map<String, Object> rows = new HashMap<>();
            rows.put(statVoColumns[0],day);
            rows.put(statVoColumns[1],order);
            rows.put(statVoColumns[2],customer);
            rows.put(statVoColumns[3],amount);
            rows.put(statVoColumns[4],pcr);

            statVo.add(rows);
        }

        HashMap<String, Object> data = new HashMap<>();
        data.put("columns",statVo.getColumns());
        data.put("rows", statVo.getRows());

        return ResponseUtil.ok(data);
    }

    //商品统计
    @GetMapping("/goods")
    public Object goods(){
        StatVo statVo = new StatVo();
        String[] columns = {"day","orders","products","amount"};
        statVo.setColumns(columns);
        String[] statVoColumns = statVo.getColumns();
        //查询所有天数
        List<String> days = statService.findGoodsDay();
        //遍历days
        for (String day : days) {
            //通过day查询每天的订单量
            List<Integer> order = statService.findOrderByDay(day);
            //通过day查询每天的下单货品数量
            List<Integer> product = statService.findProductByDay(day);
            //通过day查询每天的下单货品总额
            List<Double> amount = statService.findAmountsByDay(day);

            Map<String, Object> rows = new HashMap<>();
            rows.put(statVoColumns[0],day);
            rows.put(statVoColumns[1],order);
            rows.put(statVoColumns[2],product);
            rows.put(statVoColumns[3],amount);

            statVo.add(rows);
        }

        HashMap<String, Object> data = new HashMap<>();
        data.put("columns",statVo.getColumns());
        data.put("rows", statVo.getRows());

        return ResponseUtil.ok(data);
    }

}
